package com.yksj.healthtalk.comm;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;

/**
 * 
 * 聊天收费设定
 * @author zhao
 *
 */
public class ChargeDialogFragment extends DialogFragment implements OnClickListener{
	
	private String mTime;
	private String mPrice;
	private String mChatId;
	private String mOrid;
	private int payState = 0;//是否支付 1支付,0取消
	
	TextView timeTextV;
	TextView priceTextV;
	OnClickListener mListener;
	
	/**
	 * @param manager
	 * @param content 文字内容
	 */
	public static void showDialog(FragmentManager manager,String time,String price,String chatId,String orid){
		Fragment fragment = manager.findFragmentByTag("charge");
		FragmentTransaction ft = manager.beginTransaction();
		if(fragment != null){
			ft.remove(fragment);
		}
		DialogFragment dialog = new ChargeDialogFragment(time,price,chatId,orid,null);
		ft.add(dialog,"charge");
		ft.commitAllowingStateLoss();
	}
	
	
	public static void dismiss(FragmentManager fragmentManager){
		if(fragmentManager == null)return;
		ChargeDialogFragment fragment = (ChargeDialogFragment)fragmentManager.findFragmentByTag("charge");
		if(fragment != null){
			fragment.dismissAllowingStateLoss();
		}
	}
	
	public ChargeDialogFragment(String time,String price,String chatId,String orid,OnClickListener listener) {
		this.mTime = time;
		this.mPrice = price;
		this.mChatId = chatId;
		this.mOrid = orid;
		this.mListener = listener;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog dialog = new Dialog(getActivity(),R.style.translucent_dialog);
		dialog.setContentView(R.layout.charge_dialog_layout);
		timeTextV = (TextView)dialog.findViewById(R.id.time);
		priceTextV = (TextView)dialog.findViewById(R.id.price);
		dialog.findViewById(R.id.exit).setOnClickListener(this);
		dialog.findViewById(R.id.ok).setOnClickListener(this);
		String timeStr = "服务时间:%1$s分钟";
		String priceStr = "服务价格:%1$s多美币";
		timeStr = String.format(timeStr,mTime);
		priceStr = String.format(priceStr,mPrice);
		timeTextV.setText(timeStr);
		priceTextV.setText(priceStr);

		return dialog;
	}
	@Override
	public void onDismiss(DialogInterface dialog) {
		super.onDismiss(dialog);
		if(payState == 0){//取消支付
			SmartFoxClient.sendDoctorCharge(mTime,mPrice,mChatId,null,1);
		}else{//支付
			SmartFoxClient.sendDoctorCharge(mTime,mPrice,mChatId,mOrid,1);
		}
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.exit://取消
			dismissAllowingStateLoss();
			break;
		case R.id.ok://收费
			payState = 1;
			dismissAllowingStateLoss();
			break;
		}
	}
	
}
